//
//  DHHomepage_HeadCell.m
//  Dresshere
//
//  Created by ZhangPeng on 2018/6/3.
//  Copyright © 2018年 张朋. All rights reserved.
//

#import "DHHomepage_HeadCell.h"

@implementation DHHomepage_HeadCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.headImageView.layer.cornerRadius = self.headImageView.height / 2;
    self.headImageView.layer.borderWidth = 4;
    self.headImageView.layer.borderColor = [[UIColor whiteColor] CGColor];
    self.followView.layer.cornerRadius = self.followView.height / 2;
    self.followView.layer.borderWidth = 1;
    self.followView.layer.borderColor = [[UIColor colorWithHexString:@"#9A9A9A"] CGColor];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)updateCell:(NSDictionary *)dic{
    self.userDic = [NSMutableDictionary dictionaryWithDictionary:dic];
    [self.headImageView sd_setImageWithURL:[dic objectForKey:@"photo"]];
    [self.coverImgView dh_setImageURLString:[dic objectForKey:@"cover"]];
    self.nameLabel.text = [dic objectForKey:@"nickname"];
    self.disLabel.text = [dic objectForKey:@"title"];
    if ([[self.userDic objectForKey:@"_id"] isEqualToString:[Login curLoginUser]._id]) {
        self.followView.hidden = YES;
    }else{
        self.followView.hidden = NO;
        if ([[self.userDic objectForKey:@"subscribed"] boolValue]) {
            self.followLabel.text = @"已关注";
            self.followImageView.image = [UIImage imageNamed:@"followed"];
        }else{
            self.followLabel.text = @"关注";
            self.followImageView.image = [UIImage imageNamed:@"follow"];
        }
    }
}

- (IBAction)followAction:(id)sender {
    if ([[self.userDic objectForKey:@"subscribed"] boolValue]) {
        NSString *user_id = [self.userDic objectForKey:@"_id"];
        NSDictionary *params = @{@"user_id":user_id,@"subscribe":[NSNumber numberWithBool:NO]};
        [[DHNetAPIClient shareDHNetAPIClient] request_social_subscribeWithParams:params andBlock:^(id data, NSError *error) {
            [self.userDic setValue:@(0) forKey:@"subscribed"];
            self.followLabel.text = @"关注";
            self.followImageView.image = [UIImage imageNamed:@"follow"];
        }];
    }else{
        NSString *user_id = [self.userDic objectForKey:@"_id"];
        NSDictionary *params = @{@"user_id":user_id,@"subscribe":[NSNumber numberWithBool:YES]};
        [[DHNetAPIClient shareDHNetAPIClient] request_social_subscribeWithParams:params andBlock:^(id data, NSError *error) {
            [self.userDic setValue:@(1) forKey:@"subscribed"];
            self.followLabel.text = @"已关注";
            self.followImageView.image = [UIImage imageNamed:@"followed"];
        }];
    }
}

@end
